package com.zhc.dejingapi.service.upload.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.zhc.dejingapi.entity.QingAppinfoEntity;
import com.zhc.dejingapi.mapper.QingAppfieldsMapper;
import com.zhc.dejingapi.mapper.QingAppinfoMapper;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appinfo.QingAppInfoModel;
import com.zhc.dejingapi.pojo.qingflow.appinfo.QingAppInfoResultMainModel;
import com.zhc.dejingapi.service.upload.IQingAppInfoService;
import com.zhc.dejingapi.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class QingAppInfoServiceImpl  implements IQingAppInfoService {


    //https://blog.csdn.net/weixin_39973810/article/details/83722356

    //https://www.jianshu.com/p/dd88c9fd3a3f


    @Autowired
    private QingAppinfoMapper qingAppinfoMapper;

    @Autowired
    private QingAppfieldsMapper qingAppfieldsMapper;

    @Autowired
    QingflowApiInfo qqingflowApiInfo;

    private final JdbcTemplate mysqlJdbcTemplate;

    private final RedisTemplate<String,Object> redisTemplate;


    public QingAppInfoServiceImpl(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate1,
                                  @Qualifier("redisTemplateWin")RedisTemplate<String, Object> redisTemplate1) {
        this.redisTemplate = redisTemplate1;
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;


    }
    @Override
    public boolean SynAppInfo() {


        String strUrl = qqingflowApiInfo.getServerUrl();
        strUrl+= qqingflowApiInfo.getBaseUrl();
        strUrl+="app";
        //app?userId=2052632&favourite=0
        strUrl+="?userId="+qqingflowApiInfo.getUserId()+"&favourite=0";

        //Dictionary<string, string> dicHead = new Dictionary<string, string>();
        //dicHead.Add("accessToken", strQingAccessToken);//59d96acf-850d-49f7-9a0c-d65db32b6034
        //dicHead.Add("favourite", "0");



        try{
            Map<String,String> mapHead=new HashMap<>();
            mapHead.put("accessToken", qqingflowApiInfo.getSupperAccesstoken());
            String strBack= HttpClientUtil.doGet(strUrl,mapHead);
            log.info("返回结果："+strBack);
            Gson gson=new Gson();
            QingAppInfoResultMainModel mainModel=gson.fromJson(strBack,QingAppInfoResultMainModel.class);
            //QingAppInfoResultMainModel mainModel= JSON.parseObject(strBack,QingAppInfoResultMainModel.class);
            if (mainModel.getErrCode()==0){
                if (mainModel.getResult()==null||mainModel.getResult().getAppList()==null
                        ||mainModel.getResult().getAppList().isEmpty()||mainModel.getResult().getAppList().size()==0){
                    log.info("错误：获取的数据为空！");
                }
                else{

                    for (int i = 0; i < mainModel.getResult().getAppList().size(); i++) {
                        QingAppInfoModel infoModel=mainModel.getResult().getAppList().get(i);
                        QueryWrapper<QingAppinfoEntity> queryWrapper=new QueryWrapper<>();
                        queryWrapper.eq("appKey",infoModel.getAppKey());
                        Long iCount=qingAppinfoMapper.selectCount(queryWrapper);
                         if(iCount<1){
                             QingAppinfoEntity infoAddModel=new QingAppinfoEntity();
                             infoAddModel.setAppKey(infoModel.getAppKey());
                             infoAddModel.setAppType(0);
                             infoAddModel.setAppName(infoModel.getAppName());
                             infoAddModel.setIsDelete(0);
                             infoAddModel.setIsNeedSyn(0);
                             qingAppinfoMapper.insert(infoAddModel);
                         }
                         else{
                             QingAppinfoEntity infoEditModel=qingAppinfoMapper.selectOne(queryWrapper);
                             infoEditModel.setAppName(infoModel.getAppName());
                             qingAppinfoMapper.updateById(infoEditModel);

                         }
                    }
                }

            }
            else{
                log.info("错误：\n"+mainModel.getErrCode()+"\n"+mainModel.getErrMsg());
            }

        }
        catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage()+"\n"+e.getStackTrace());

        }


        return false;
    }

}
